﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Beway.ValueAdded.Service {
	 public class ReportServ {
		 /// <summary>
		 /// 商户报表
		 /// </summary>
		 /// <param name="beginTime"></param>
		 /// <param name="endTime"></param>
		 /// <returns></returns>
		 public List<ReportView> GetReportData(DateTime? beginTime,DateTime? endTime,int adminId) {
			 using (BewayValueAddedEntities db=new BewayValueAddedEntities ()) {
                SqlParameter paramBeginTime = new SqlParameter("@beginTime", beginTime);
                SqlParameter paramEndTime = new SqlParameter("@endTime", endTime);
                string sql = @"select u.UserTitle,tt.* from (select t.UserID,max(case(rctype) when 1 then total else 0 end)  as 'mobile',max(case(rctype) when 2 then total else 0 end) as 'qq', 
max(case(rctype) when 3 then total else 0 end) AS 'gift',max(case(rctype) when 4 then total else 0 end) AS 'flowpacket',max(case(rctype) when 5 then total else 0 end) as 'WeiXinHB'  from (select COUNT(0) 'total',userid,RCType from recharge where OrderTime>@beginTime and OrderTime<@endTime 
group by userid,RCType) as t inner join ReUser u on t.UserID = u.UserID  group by  t.userid)
as tt inner join ReUser u on tt.UserID = u.UserID and u.RechStatus=1";
                if (adminId > 0)
                {
                    sql += " and u.AdminId=" + adminId.ToString();
                }

                var q = db.Database.SqlQuery<ReportView>(sql, new SqlParameter[] { paramBeginTime, paramEndTime }).ToList();
                return q;
            }
		 }
		 /// <summary>
		 /// 获取某个商户的具体充值类型的报表数据
		 /// </summary>
		 /// <param name="rcType">充值类型</param>
		 /// <param name="userID">商户ID</param>
		 /// <returns></returns>
		 public List<ReportView> QueryReportDetails(string rcType,string userId,DateTime? beginTime,DateTime? endTime) {
			 using (BewayValueAddedEntities db = new BewayValueAddedEntities()) {
				 SqlParameter paramRctype = new SqlParameter("@RCType", rcType);
				 SqlParameter paramUserID = new SqlParameter("@UserID", userId);
				 SqlParameter paramBeginTime = new SqlParameter("@beginTime", beginTime);
				 SqlParameter paramEndTime = new SqlParameter("@endTime", endTime);
				 var q = db.Database.SqlQuery<ReportView>(@"select RechargeAmount,COUNT(RechargeAmount) as TypeCount from Recharge,ReUser
where Recharge.UserID=ReUser.UserID and RCType=@RCType and ReUser.UserID=@UserID and OrderTime>@beginTime and OrderTime<@endTime
group by RechargeAmount", new SqlParameter[]{paramRctype,paramUserID,paramBeginTime,paramEndTime}).ToList();
				 return q;
			 }
		 }
	}
}
